package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.services.EventService;

/**
 * Servlet implementation class EventRegistrationServlet
 */
@WebServlet("/EventRegistrationServlet")
public class EventRegistrationServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	 /**
     * @see HttpServlet#HttpServlet()
     */
    public EventRegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventRegistration.jsp");  
		rd.forward(request,response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");    
	       
		HttpSession session=request.getSession(true);
		 
		Event event=new Event();
		
		event.setEventName(request.getParameter("eventname"));  
		event.setDescription(request.getParameter("description")); 
		event.setPlace(request.getParameter("place"));  
		event.setDuration(Integer.parseInt(request.getParameter("duration")));
		event.setEventType(request.getParameter("eventtype"));  
		event.setSeatsTotal(Integer.parseInt(request.getParameter("totaltickets")));
		event.setSeatsAvailable(Integer.parseInt(request.getParameter("totaltickets")));
	   
	    
	    EventService es=new EventService();
	    
	    try {
	    	if(es.checkEvent(event)==false){
	    		
	    		if(es.registerNewEvent(event)==1){
				RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
    			rd.forward(request,response);
	    		}
	    		else {
	    			session.setAttribute("error", "Event not registered");
		    		 
		    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventRegistration.jsp");  
		    		rd.include(request,response);
	    		
	    		}
				
	    	}
	    	
	    	else{
	    		session.setAttribute("error", "This event exists");
	    		 
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventRegistration.jsp");  
	    		rd.include(request,response);
	    	}
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }
	          
	}

}
